﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using RestfulApiV1.Data.Entity;
using RestfulApiV1.Domain.Configure;
using RestfulApiV1.Domain.DTOs;
using RestfulApiV1.Domain.DTOs.Token;
using RestfulApiV1.Implement.Paged;
using RestfulApiV1.Interface.Data;
using RestfulApiV1.Utils.Auth;
using RestfulApiV1.Utils.Json;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RestfulApiV1.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ArticlesController : ControllerBase
    {
        private readonly IRespository<Articles> _articlesRespsitory;
        private readonly IWebHostEnvironment _env;
        private TokenParameter _tokenParameter = new TokenParameter();
        private readonly IConfiguration _configuration;

        public ArticlesController(IRespository<Articles> articlesRespsitory,
           IWebHostEnvironment env, IConfiguration configuration)
        {
            _articlesRespsitory = articlesRespsitory;
            _env = env;
            _configuration = configuration;

            var config = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .Build();

            _tokenParameter = config.GetSection("TokenParameter").Get<TokenParameter>();
        }
        // GET: api/<ArticlesController>
        [HttpGet]
        public string GetArticles(int pageIndex = 1, int pageSize = 10)
        {
            var list = _articlesRespsitory.Table
                .Where(x => x.IsDeleted == false);

            //var resData = new
            //{
            //    PageIndex=pageIndex,
            //    PageSize=pageSize,
            //    TotalItem=totalItem,
            //    TotalPage=totalPage,
            //    Data= new Pagination<Users>(list,pageIndex,pageSize)
            //};

            var res = new
            {
                Code = 1000,
                Data = GetDynamic(new Pagination<Articles>(list, pageIndex, pageSize)),
                Msg = "获取文章列表成功"
            };

            return JsonHelper.SerializeObject(res);
        }

        // GET api/<ArticlesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            var article = _articlesRespsitory.GetById(id);
            var res = new
            {
                Code = 1000,
                Data = GetDynamic(article),
                Msg = "获取指定文章成功",
            };
            return JsonHelper.SerializeObject(res);
        }

        // POST api/<ArticlesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<ArticlesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ArticlesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }

        private dynamic GetDynamic(Articles articles)
        {
            var res = new
            {
                articles.ArticleName,
                articles.ArticleFragment,
                articles.ArticlesTypesId
            };

            return res;
        }

        //private dynamic GetDynamic(Pagination<ArticleCategory> categories)
        //{
        //    var list = new List<dynamic>();
        //    foreach (var category in categories.List)
        //    {
        //        var temp = GetDynamic(category);
        //        list.Add(temp);
        //    }
        //    var data = new
        //    {
        //        categories.PageIndex,
        //        categories.PageSize,
        //        categories.TotalItem,
        //        categories.TotalPage,
        //        List = list
        //    };
        //    return data;
        //}

        private dynamic GetDynamic(Pagination<Articles> articles)
        {
            var list = new List<dynamic>();
            foreach (var article in articles.List)
            {
                var temp = GetDynamic(article);
                list.Add(temp);
            }
            var data = new
            {
                articles.PageIndex,
                articles.PageSize,
                articles.TotalItem,
                articles.TotalPage,
                List=list
            };
            return data;
        }

    }
}
